<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>webgl着色器</title>
  <script src="lib/webgl-debug.js"></script>
  <script src="lib/webgl-utils.js"></script>
  <script src="lib/cuon-utils.js"></script>
  <script src="lib/cuon-matrix.js"></script>
</head>

<body onload="main()">
  <canvas id="canvas" width="400" height="490"></canvas>
</body>
<script>
  // 顶点着色器伪代码
  var vShder_source = `
attribute vec4 position;
uniform float psize;
void main(){
gl_Position =position;
gl_PointSize = psize;
}
`
  //片元着色器伪代码
  var fShder_source = `
  precision mediump float;
  uniform float width;
  uniform float height;
void main(){
gl_FragColor = vec4(gl_FragCoord.x/width,1,gl_FragCoord.y/height,1); //设置颜色
// gl_FragColor = vec4(1,1,1,1); //设置颜色
}
`
  function main() {
    var canvas = document.getElementById('canvas');
    //  获取上下文对象
    var gl = getWebGLContext(canvas);
    //  初始化着色器
    if (!initShaders(gl, vShder_source, fShder_source)) {
      return;
    }
     initVertexBuffers(gl);
    var psize = gl.getUniformLocation(gl.program, "psize");
    gl.uniform1f(psize, 20);



    var width = gl.getUniformLocation(gl.program,"width");
    gl.uniform1f(width,gl.drawingBufferWidth);
    var height = gl.getUniformLocation(gl.program,"height");
    gl.uniform1f(height,gl.drawingBufferHeight);

    // 指定一个覆盖canvas的颜色
    gl.clearColor(0, 0, 0, 1.0);
    // 清除指定的缓冲区
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, 3);
  }

  function initVertexBuffers(gl) {
    var vertexDatas = new Float32Array([
      0, 0.5,
      -0.5, -0.5,
      0.5, -0.5,
    ]);

    var vertexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertexDatas, gl.STATIC_DRAW);
    var position = gl.getAttribLocation(gl.program, "position");
    gl.vertexAttribPointer(position, 2, gl.FLOAT, false, 0, 0);
    gl.enableVertexAttribArray(position);
    return 3;
  }

</script>

</html>